package com.cmcc.qx.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.beanutils.ConvertUtils;

/**
 * 字符串工具类
 * 
 * @author zhangxiaobo
 */
public class StringUtil {

	/**
	 * 判断字符串是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNull(String s) {
		return s == null || s.length() == 0;
	}
	
	/**
	 * 将List转换为分隔符分割的字符串
	 * @param list List
	 * @param seperator 分隔符
	 * @return
	 */
	public static String listToString(List<? extends Object> list,
			String seperator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			if (i != 0) {
				sb.append(seperator);
			}
			sb.append(list.get(i));
		}
		return sb.toString();
	}
	
	/**
	 * List转换
	 * @param list list
	 * @param targetType 要转换的目标类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> listConvert(List<? extends Object> list,
			Class<T> targetType) {
		List<T> result = new ArrayList<T>();
		for (Object obj : list) {
			result.add((T) ConvertUtils.convert(obj, targetType));
		}
		return result;
	}

	/**
	 * 得到一个唯一的id字符串
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 将异常栈中的信息转换为字符串
	 * 
	 * @param t
	 * @return
	 */
	public static String stackTrace2String(Throwable t) {
		StringWriter sw = new StringWriter();
		t.printStackTrace(new PrintWriter(sw, true));
		return sw.getBuffer().toString();
	}

}
